/**	CODE EVALL::Moderate [Detecting Cycles]
 *	Given a sequence, write a program to detect cycles within it.
 *
 *	Solution by Michael Pio Arroyo
 */

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>

int main(int argc, char** argv) {
	std::ifstream file;
	std::string line;
	std::vector<int> list;
	file.open(argv[1]);

	while(getline(file, line)) {
		std::string::iterator it = line.begin();
		std::string t, s;
		// read line and convert to integer list
		while(it != line.end()) {
			if(*it != ' ') {
				t = *it;
				s.append(t.c_str());
			} else {
				list.push_back(atoi(s.c_str()));
				s.clear();
			}
			++it;
		}
		int start = 0;
		int end = 0;
		int num = 0;
		bool found = false;
		while(!found) {
			num = list.at(start);
			for(int i=start+1; i<list.size(); ++i) {
				if(num == list.at(i)) {
					found = true;
					end = i;
					break;
				}
			}
			if(!found)
				++start;
		}
		for(int i=start; i<end; ++i) {
			if(i != start)
				putchar(' ');
			std::cout << list.at(i);			
		}
	}

	file.close();
	return 0;
}